Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

The Example Servlets

This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 11-1 lists the servlets that handle each bookstore function. Each programming task is illustrated by one or more servlets. For example, BookDetailsServlet illustrates how to handle HTTP GET requests, BookDetailsServlet and CatalogServlet show how to construct responses, and CatalogServlet illustrates how to track session information.

Table 11-1 Duke's Bookstore Example Servlets 
Function
Servlet
Enter the bookstore
BookStoreServlet
Create the bookstore banner
BannerServlet
Browse the bookstore catalog
CatalogServlet
Put a book in a shopping cart
CatalogServlet,
BookDetailsServlet
Get detailed information on a specific book
BookDetailsServlet
Display the shopping cart
ShowCartServlet
Remove one or more books from the shopping cart
ShowCartServlet
Buy the books in the shopping cart
CashierServlet
Receive an acknowledgement for the purchase
ReceiptServlet

The data for the bookstore application is maintained in a database and accessed through the helper class database.BookDB. The database package also contains the class BookDetails, which represents a book. The shopping cart and shopping cart items are represented by the classes cart.ShoppingCart and cart.ShoppingCartItem, respectively.

The source code for the bookstore application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore1/ directory created when you unzip the tutorial bundle (see Building and Running the Examples). A sample bookstore1.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build, package, deploy, and run the example:

  1. Build and package the bookstore common files as described in Duke's Bookstore Examples.
  2. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/bookstore1/.
  3. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore1/build/ directory.
  4. Start the J2EE application server.
  5. Perform all the operations described in Accessing Databases from Web Applications.
  6. Start deploytool.
  7. Create a Web application called bookstore1 by running the New Web Application Wizard. Select FileRight ArrowNewRight ArrowWeb Application WAR.
  8. New Web Application Wizard
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. Click Browse and in the file chooser, navigate to <INSTALL>/docs/tutorial/examples/web/bookstore1/build/.
    3. In the File Name field, enter bookstore1.
    4. Click Choose Module File.
    5. In the WAR Display Name field, enter bookstore1.
    6. In the Context Root field, enter /bookstore1.
    7. Click Edit.
    8. In the Edit Archive Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore1/build/. Select errorpage.html, duke.books.gif, BannerServlet.class, BookStoreServlet.class, BookDetailsServlet.class, CatalogServlet.class, ShowCartServlet.class, CashierServlet.class, ReceiptServlet.class, and the database, filters, listeners, and util packages. Click Add.
    9. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and Click Add.
    10. Click OK.
    11. Click Next.
    12. Select the Servlet radio button.
    13. Click Next.
    14. Select BannerServlet from the Servlet Class combo box.
    15. Click Finish.
  9. Add each of the Web components listed in Table 11-2. For each servlet:
    1. Select FileRight ArrowNewRight ArrowWeb Application WAR.
    2. Click the Add to Existing WAR Module radio button. Since the WAR contains all of the servlet classes, you do not have to add any more content.
    3. Click Next.
    4. Select the Servlet radio button and the Component Aliases checkbox.
    5. Click Next.
    6. Select the servlet from the Servlet Class combo box.
    7. Click Next.
    8. Click Add. Enter the alias.
    9. Click Finish.
      Table 11-2 Duke's Bookstore Web Components 
      Web Component Name
      Servlet Class
      Component Alias
      BookStoreServlet
      BookStoreServlet
      /bookstore
      CatalogServlet
      CatalogServlet
      /bookcatalog
      BookDetailsServlet
      BookDetailsServlet
      /bookdetails
      ShowCartServlet
      ShowCartServlet
      /bookshowcart
      CashierServlet
      CashierServlet
      /bookcashier
      ReceiptServlet
      ReceiptServlet
      /bookreceipt
  10. Add the listener class listeners.ContextListener (described in Handling Servlet Life Cycle Events).
    1. Select the Event Listeners tab.
    2. Click Add.
    3. Select the listeners.ContextListener class from drop down field in the Event Listener Classes panel.
  11. Add an error page (described in Handling Errors).
    1. Select the File Refs tab.
    2. Click Add in the Error Mapping panel.
    3. Enter exception.BookNotFoundException in the Error/Exception field.
    4. Enter /errorpage.html in the Resource to be Called field.
    5. Repeat for exception.BooksNotFoundException and javax.servlet.UnavailableException.
  12. Add the filters filters.HitCounterFilter and filters.OrderFilter (described in Filtering Requests and Responses).
    1. Select the Filter Mapping tab.
    2. Click Edit Filter List.
    3. Click Add.
    4. Select filters.HitCounterFilter from the Filter Class column. The deploytool will automatically enter HitCounterFilter in the Display Name column.
    5. Click Add.
    6. Select filters.OrderFilter from the Filter Class column. The deploytool will automatically enter OrderFilter in the Display Name column.
    7. Click OK.
    8. Click Add.
    9. Select HitCounterFilter from the Filter Name column.
    10. Select Servlet from the Target Type column.
    11. Select BookStoreServlet from the Target column.
    12. Repeat for OrderFilter. The target type is Servlet and the target is ReceiptServlet.
  13. Add a resource reference for the database.
    1. Select the Resource Refs tab.
    2. Click Add.
    3. Enter jdbc/BookDB in the Coded Name field.
    4. Accept the default type javax.sql.DataSource.
    5. Accept the default authorization Container.
    6. Accept the default selected Shareable.
    7. Enter jdbc/BookDB in the JNDI name field of the Deployment setting for jdbc/BookDB frame.
  14. Select FileRight ArrowSave.
  15. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. In the Connection Settings frame, enter the user name and password you specified when you installed the J2EE 1.4 Application Server.
    3. Click OK.
    4. A popup dialog will display the results of the deployment. Click Close.
  16. To run the application, open the bookstore URL
    http://localhost:1024/bookstore1/bookstore.

Troubleshooting

The Duke's Bookstore database access object returns the following exceptions:

Because we have specified an error page, you will see the message The application is unavailable. Please try later. If you don't specify an error page, the Web container generates a default page containing the message A Servlet Exception Has Occurred and a stack trace that can help diagnose the cause of the exception. If you use errorpage.html, you will have to look in the server log to determine the cause of the exception. The server log file is <J2EE_HOME>/domains/domain1/server/logs/server.log.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.